package com.cssnj.portal.web.admin.positionrole;

import com.cssnj.portal.domain.common.UserInfo;
import com.cssnj.portal.service.admin.positionrole.PositionroleService;
import com.cssnj.portal.service.login.LoginService;
import com.fasterxml.jackson.databind.util.JSONPObject;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.constants.SysConstants;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.utility.IPHelper;
import io.predictech.framework.utility.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.cssnj.portal.config.SecondDataSourceConfig;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping({"/admin/positionrole"})
public class PositionroleController {
    @Autowired
    private PositionroleService positionroleService;

    @Autowired
    Environment environment;


    @RequestMapping({"/init.do"})
    public ModelAndView init(HttpServletRequest request) throws BaseException, SQLException {
        //页面初始化
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("positionroleService");
        jsonRequest.setHandleCode("init");
        JsonResponse jsonResponse = positionroleService.execute(jsonRequest);
        ModelAndView modelAndView = new ModelAndView("admin/positionrole/positionrole");
        if (null != jsonResponse.getRepData().get("gwflList")) {
            modelAndView.getModelMap().put("gwflList", jsonResponse.getRepData().get("gwflList"));
        }
        if (null != jsonResponse.getRepData().get("sjDm")) {
            modelAndView.getModelMap().put("sjDm", jsonResponse.getRepData().get("sjDm"));
        }
        if (null != jsonResponse.getRepData().get("sjList")) {
            modelAndView.getModelMap().put("sjList", jsonResponse.getRepData().get("sjList"));
        }
        return modelAndView;
    }

    @RequestMapping({"/queryGwLb.do"})
    @ResponseBody
    public String queryGwLb(HttpServletRequest request) throws BaseException, SQLException {
        // HashMap<String, Object> map = new HashMap<String, Object>();
        //根据条件查询岗位列表
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("positionroleService");
        jsonRequest.setHandleCode("queryGwLb");
        jsonRequest.getReqData().put("sjlb", request.getParameter("sjlb"));
        jsonRequest.getReqData().put("gwfl", request.getParameter("gwfl"));
        jsonRequest.getReqData().put("page", request.getParameter("page"));
        jsonRequest.getReqData().put("rows", request.getParameter("rows"));
        jsonRequest.getReqData().put("jsnr", request.getParameter("jsnr"));
        JsonResponse jsonResponse = positionroleService.execute(jsonRequest);
        String str = (String) jsonResponse.getRepData().get("str");
        return str;
    }

    @RequestMapping({"/queryJsByGwDm.do"})
    @ResponseBody
    public JsonResponse queryJsByGwDm(HttpServletRequest request) throws BaseException, SQLException {
        //根据岗位代码查询角色树
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("positionroleService");
        jsonRequest.setHandleCode("queryJsByGwDm");
        jsonRequest.getReqData().put("gwdm", request.getParameter("gwdm"));
        JsonResponse jsonResponse = positionroleService.execute(jsonRequest);
        return jsonResponse;
    }

    @RequestMapping({"/saveGwJs.do"})
    @ResponseBody
    public JsonResponse saveGwJs(HttpServletRequest request) throws BaseException, SQLException {
        //根据岗位代码查询角色树
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("positionroleService");
        jsonRequest.setHandleCode("saveGwJs");
        jsonRequest.getReqData().put("gwdm", request.getParameter("gwdm"));
        jsonRequest.getReqData().put("nodes", request.getParameter("nodes"));
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        jsonRequest.getReqData().put("swrydm", userInfo.getSwryDm());
        JsonResponse jsonResponse = positionroleService.execute(jsonRequest);
        return jsonResponse;
    }
}
